
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>6.1 eventLoop工作机制 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="62zi-dong-shi-jian.html" />
    
    
    <link rel="prev" href="../6shi-jian-xun-huan.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    序言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1.html">
            
                <a href="../chapter1.html">
            
                    
                    1. 快速入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../2ji-ben-zu-jian.html">
            
                <a href="../2ji-ben-zu-jian.html">
            
                    
                    2.基本组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../2ji-ben-zu-jian/processor.html">
            
                <a href="../2ji-ben-zu-jian/processor.html">
            
                    
                    2.1 Processor
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../2ji-ben-zu-jian/22-pipline.html">
            
                <a href="../2ji-ben-zu-jian/22-pipline.html">
            
                    
                    2.2 Pipline
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../2ji-ben-zu-jian/23-sessionpool.html">
            
                <a href="../2ji-ben-zu-jian/23-sessionpool.html">
            
                    
                    2.3 SessionPool
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../3pa-chong-kong-zhi.html">
            
                <a href="../3pa-chong-kong-zhi.html">
            
                    
                    3.爬虫控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../4chou-qu-qi.html">
            
                <a href="../4chou-qu-qi.html">
            
                    
                    4.抽取器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../4chou-qu-qi/41sipsoup.html">
            
                <a href="../4chou-qu-qi/41sipsoup.html">
            
                    
                    4.1SipSoup
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../4chou-qu-qi/42tableevaluator.html">
            
                <a href="../4chou-qu-qi/42tableevaluator.html">
            
                    
                    4.2TableEvaluator
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../5zhong-zi-guan-li-qi.html">
            
                <a href="../5zhong-zi-guan-li-qi.html">
            
                    
                    5.种子管理器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../6shi-jian-xun-huan.html">
            
                <a href="../6shi-jian-xun-huan.html">
            
                    
                    6.事件循环
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.7.1" data-path="61-eventloopgong-zuo-ji-zhi.html">
            
                <a href="61-eventloopgong-zuo-ji-zhi.html">
            
                    
                    6.1 eventLoop工作机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="62zi-dong-shi-jian.html">
            
                <a href="62zi-dong-shi-jian.html">
            
                    
                    6.2 自动事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="63delayevent.html">
            
                <a href="63delayevent.html">
            
                    
                    6.3 延时事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="64classScanner.html">
            
                <a href="64classScanner.html">
            
                    
                    6.4 classScanner
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >6.1 eventLoop工作机制</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5148;&#x770B;&#x56FE;<img src="../assets/eventLoop.png" alt="">&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x5F00;&#x542F;&#x4E4B;&#x540E;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5FAA;&#x73AF;&#x7684;&#x4ECE;delayQueue&#x4E2D;&#x83B7;&#x5F97;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x4E8B;&#x4EF6;&#x67E5;&#x627E;&#x4ED6;&#x7684;&#x5904;&#x7406;&#x5668;&#xFF0C;&#x4E4B;&#x540E;&#x5FAA;&#x73AF;&#x56DE;&#x8C03;&#x6BCF;&#x4E2A;&#x5904;&#x7406;&#x5668;&#x3002;&#x5B8C;&#x6210;&#x540E;&#x7EE7;&#x7EED;&#x53BB;&#x62FF;&#x4E8B;&#x4EF6;&#xFF0C;&#x518D;&#x6B21;&#x5FAA;&#x73AF;&#x5904;&#x7406;&#x3002;&#x7136;&#x540E;&#x53EF;&#x80FD;&#x6709;&#x591A;&#x4E2A;&#x4E8B;&#x4EF6;&#x6295;&#x9012;&#x65B9;&#xFF0C;&#x4ED6;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x65F6;&#x523B;&#x6295;&#x9012;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5728;delayQueue &#x4E2D;</p>
<h2 id="event">event</h2>
<p>event &#x662F;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x662F;&#x7528;&#x6765;&#x6295;&#x9012;&#x5230;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#x3002;&#x5B83;&#x5305;&#x542B;&#x5982;&#x4E0B;&#x5C5E;&#x6027;&#xFF1A;</p>
<ol>
<li>topic &#x4EE3;&#x8868;&#x4E8B;&#x4EF6;&#x4E3B;&#x9898;&#xFF0C;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x5668;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x4E3B;&#x9898;&#x6765;&#x786E;&#x5B9A;&#x80FD;&#x591F;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x5224;&#x65AD;&#x54EA;&#x4E9B;&#x5904;&#x7406;&#x5668;&#x6765;&#x5904;&#x7406;&#x672C;&#x4E8B;&#x4EF6;</li>
<li>what &#x4E8B;&#x4EF6;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x53D1;&#x73B0;&#x8FD9;&#x4E2A;&#x8BBE;&#x8BA1;&#x76EE;&#x524D;&#x673A;&#x4F1A;&#x6CA1;&#x6709;&#x88AB;&#x4F7F;&#x7528;&#x5230;</li>
<li>data &#x4E8B;&#x4EF6;&#x6240;&#x643A;&#x5E26;&#x7684;&#x53C2;&#x6570;&#x4FE1;&#x606F;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E8B;&#x4EF6;&#x7684;&#x5177;&#x4F53;&#x5185;&#x5BB9;&#xFF0C;&#x76EE;&#x524D;&#x662F;&#x4E00;&#x4E2A;object&#xFF0C;&#x53EF;&#x4EE5;&#x5B58;&#x653E;&#x4EFB;&#x4F55;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;</li>
<li>time &#x4E8B;&#x4EF6;&#x88AB;&#x6FC0;&#x53D1;&#x7684;&#x65F6;&#x95F4;&#x70B9;&#xFF0C;&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x4ED6;&#xFF0C;&#x53EF;&#x4EE5;&#x6807;&#x8BB0;&#x6539;&#x4E8B;&#x4EF6;&#x5728;&#x672A;&#x6765;&#x67D0;&#x4E2A;&#x65F6;&#x95F4;&#x70B9;&#x624D;&#x56DE;&#x88AB;&#x89E6;&#x53D1;&#xFF0C;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x5EF6;&#x65F6;&#x4E8B;&#x4EF6;</li>
</ol>
<h2 id="eventhandler">EventHandler</h2>
<p>EventHandler&#x662F;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x5668;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x4F60;&#x7684;&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x80FD;&#x591F;&#x5904;&#x7406;&#x67D0;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.virjar.vscrawler.core.event;

<span class="hljs-comment">/**
 * Created by virjar on 17/4/30.
 * <span class="hljs-doctag">@author</span> virjar
 * <span class="hljs-doctag">@since</span> 0.0.1
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">EventHandler</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">handEvent</span><span class="hljs-params">(Event event)</span></span>;
}
</code></pre>
<p>&#x63A5;&#x53E3;&#x5F88;&#x7B80;&#x5355;</p>
<h2 id="demo">demo</h2>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EventTest</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        EventLoop.getInstance().loop();

        AutoEventRegistry eventRegister = AutoEventRegistry.getInstance();

        eventRegister.registerObserver(<span class="hljs-keyword">new</span> UserLoginEvent() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">afterUserLogin</span><span class="hljs-params">(User user, <span class="hljs-keyword">boolean</span> loginSucces)</span> </span>{
                System.out.println(Thread.currentThread() + <span class="hljs-string">&quot;&#x7528;&#x6237;&#x767B;&#x5F55;:&quot;</span> + (loginSucces ? <span class="hljs-string">&quot;&#x6210;&#x529F;&quot;</span> : <span class="hljs-string">&quot;&#x5931;&#x8D25;&quot;</span>));
            }
        });

        UserLoginEvent userLoginEvent = eventRegister.findEventDeclaring(UserLoginEvent.class);
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
            userLoginEvent.afterUserLogin(<span class="hljs-keyword">null</span>, <span class="hljs-keyword">false</span>);
        }

        CommonUtil.sleep(<span class="hljs-number">20000</span>);
    }
}
</code></pre>
<p>&#x662F;&#x4E0D;&#x662F;&#x8FD8;&#x662F;&#x89C9;&#x5F97;&#x6709;&#x70B9;&#x8FF7;&#x7CCA;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x89C9;&#x5F97;&#x5B58;&#x7CB9;&#x7684;event&#x6765;&#x5904;&#x7406;&#x7528;&#x8D77;&#x6765;&#x5F88;&#x4E0D;&#x65B9;&#x4FBF;&#xFF0C;&#x6240;&#x4EE5;&#x53C8;&#x505A;&#x4E86;&#x4E00;&#x6B21;&#x5C01;&#x88C5;&#x3002;&#x8BF7;&#x770B;&#x4E0B;&#x8282;&#xFF1A;&#x81EA;&#x52A8;&#x4E8B;&#x4EF6;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../6shi-jian-xun-huan.html" class="navigation navigation-prev " aria-label="Previous page: 6.事件循环">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="62zi-dong-shi-jian.html" class="navigation navigation-next " aria-label="Next page: 6.2 自动事件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"6.1 eventLoop工作机制","level":"1.7.1","depth":2,"next":{"title":"6.2 自动事件","level":"1.7.2","depth":2,"path":"6shi-jian-xun-huan/62zi-dong-shi-jian.md","ref":"6shi-jian-xun-huan/62zi-dong-shi-jian.md","articles":[]},"previous":{"title":"6.事件循环","level":"1.7","depth":1,"path":"6shi-jian-xun-huan.md","ref":"6shi-jian-xun-huan.md","articles":[{"title":"6.1 eventLoop工作机制","level":"1.7.1","depth":2,"path":"6shi-jian-xun-huan/61-eventloopgong-zuo-ji-zhi.md","ref":"6shi-jian-xun-huan/61-eventloopgong-zuo-ji-zhi.md","articles":[]},{"title":"6.2 自动事件","level":"1.7.2","depth":2,"path":"6shi-jian-xun-huan/62zi-dong-shi-jian.md","ref":"6shi-jian-xun-huan/62zi-dong-shi-jian.md","articles":[]},{"title":"6.3 延时事件","level":"1.7.3","depth":2,"path":"6shi-jian-xun-huan/63delayevent.md","ref":"6shi-jian-xun-huan/63delayevent.md","articles":[]},{"title":"6.4 classScanner","level":"1.7.4","depth":2,"path":"6shi-jian-xun-huan/64classScanner.md","ref":"6shi-jian-xun-huan/64classScanner.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"6shi-jian-xun-huan/61-eventloopgong-zuo-ji-zhi.md","mtime":"2017-06-25T02:34:15.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-25T14:04:12.299Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

